package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.entity.SysRole;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.contest.entity.SysMenu;
import com.contest.service.SysMenuService;


/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2017-12-20 16:30:37
 */

@Controller
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Autowired
    SysMenuService sysMenuService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String, Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<SysMenu> mPage = new Page<SysMenu>(page, rows);

        Wrapper<SysMenu> w = new EntityWrapper<>();
        String o = String.valueOf(param.get("name"));
        if(o != null && !o.equals("") && !o.equals("null")) {
            String name = String.valueOf(o);
            w.like("fd_name",name);
        }

        mPage = sysMenuService.selectListByPage(mPage);
        return new PageUtils(mPage.getRecords(), mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(SysMenu sysMenu) {
        sysMenu.setFdId(idWorker.nextId() + "");
        if (sysMenuService.insert(sysMenu)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @RequestMapping("/comboList/{type}")
    @ResponseBody
    public List<SysMenu> comboList(@PathVariable("type") Integer type) {
        EntityWrapper<SysMenu> menuEntityWrapper = new EntityWrapper<SysMenu>();
        if (type == 0) {
            menuEntityWrapper.isNull("fd_parent");
            menuEntityWrapper.or("fd_parent={0}", 0);
        }
        List<SysMenu> sysMenuList = sysMenuService.selectList(menuEntityWrapper);
        return sysMenuList;
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        SysMenu sysMenu = sysMenuService.selectById(fdId);
        model.addAttribute("sysMenu", sysMenu);
        return "sysMenu/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (sysMenuService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(SysMenu sysMenu) {
        if (sysMenuService.updateById(sysMenu)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }

}
